package org.maxgroundstation.groundstation.view.map;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.maxgroundstation.groundstation.GroundStation;
import org.maxgroundstation.groundstation.GroundStationEvent;
import org.maxgroundstation.groundstation.GroundStationListener;
import org.maxgroundstation.groundstation.comm.service.ServiceEvent;
import org.maxgroundstation.groundstation.comm.service.ServiceListener;
import org.maxgroundstation.groundstation.comm.service.gps.GpsService;
import org.maxgroundstation.groundstation.comm.service.gps.PointOfInterest;

/**
 * A map overlay for displaying points of interest.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class PointsOfInterestMapOverlay implements MapOverlay, GroundStationListener, ServiceListener {

    // The overlay ID.
    public static final String OVERLAY_ID = "Points of Interest";
    private static final Color FG_COLOR = Color.YELLOW;
    private static final Color BG_COLOR = new Color(0, 0, 0, 127);
    private static final Stroke FG_STROKE = new BasicStroke(2F);
    private static final Stroke BG_STROKE = new BasicStroke(4F);
    private static final int DISPLAY_RANGE = 5000;
    
    // Data members.
    private List<PointOfInterest> poiCache;
    
    /**
     * Constructor
     * @param gs the ground station.
     */
    public PointsOfInterestMapOverlay(GroundStation gs) {
        
        // Initialize data members.
        poiCache = new ArrayList<PointOfInterest>();
        
        // Add as ground station listener.
        gs.addGroundStationListener(this);
    }
    
    @Override
    public void drawMapOverlay(String baseMapStyle, Graphics2D g2d, GpsLocation currentLoc, int width, 
            int height, float zoom) {
        
        int xCenter = width / 2;
        int yCenter = height / 2;
        
        Stroke saveStroke = g2d.getStroke();
        
        // Draw points of interest.
        if (currentLoc != null) {
            
            List<PointOfInterest> tempPoiList = new ArrayList<PointOfInterest>(poiCache);
            Iterator<PointOfInterest> i = tempPoiList.iterator();
            while (i.hasNext()) {
                PointOfInterest poi = i.next();
                GpsLocation poiGpsLoc = new GpsLocation(poi.getLatitude(), poi.getLongitude());
                int poiLocX = xCenter - (int) (MapUtil.getXDiff(poiGpsLoc, currentLoc) / zoom);
                int poiLocY = (int) (MapUtil.getYDiff(poiGpsLoc, currentLoc) / zoom) + yCenter;
                
                int xDiff = Math.abs(xCenter - poiLocX);
                int yDiff = Math.abs(yCenter - poiLocY);
                if ((xDiff < DISPLAY_RANGE) && (yDiff < DISPLAY_RANGE)) {
                
                    // Draw each point of interest as yellow triangle.
                    int[] triangleX = {poiLocX - 3, poiLocX, poiLocX + 3};
                    int[] triangleY = {poiLocY + 3, poiLocY - 3, poiLocY + 3};
                    
                    g2d.setColor(BG_COLOR);
                    g2d.setStroke(BG_STROKE);
                    g2d.drawPolygon(triangleX, triangleY, 3);
                    
                    g2d.setColor(FG_COLOR);
                    g2d.setStroke(FG_STROKE);
                    g2d.drawPolygon(triangleX, triangleY, 3);
                }
            }
        }
        
        g2d.setStroke(saveStroke);
    }

    @Override
    public void groundStationUpdate(GroundStationEvent event) {
        // If services initialized from rover, check for GPS service.
        GroundStation gs = (GroundStation) event.getSource();
        if (GroundStationEvent.SERVICES_INITIALIZED.equals(event.getType())) {
            if (gs.getServiceComm().hasService(GpsService.SERVICE_ID)) {
                // Add as GPS service listener.
                GpsService gpsService = (GpsService) gs.getServiceComm().getService(
                    GpsService.SERVICE_ID);
                gpsService.addServiceListener(this);
                
                // Populate points of interest cache.
                poiCache = gpsService.getPointsOfInterest();
            }
        }
    }

    @Override
    public void serviceUpdate(ServiceEvent event) {
        if (event.getType().equals(GpsService.POI_UPDATE)) {
            // Update point of interest cache from GPS service.
            poiCache = (List<PointOfInterest>) event.getTarget();
        }
    }

    @Override
    public boolean isDraggable() {
        return true;
    }
}